package leetcode.singles;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @Author: Liu Yue
 * @Descripition:
 * @Date; Create in 2020/3/1 15:47
 **/
public class SeriableSingletonTest {

    public static void main(String[] args) {
        RegisterSingle instance1=null;
        RegisterSingle instance = RegisterSingle.getInstance();
        FileOutputStream fos = null;
        //序列化和反序列化破坏单例
        try{
            fos = new FileOutputStream("containerSingleton.obj");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(instance);
            oos.flush();
            oos.close();

            FileInputStream fis = new FileInputStream("containerSingleton.obj");
            ObjectInputStream ois = new ObjectInputStream(fis);
            instance1 = (RegisterSingle)ois.readObject();
            ois.close();

            System.out.println(instance1);
            System.out.println(instance);
            System.out.println(instance == instance1);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
